package com.lzx.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.shop.dao.AddressDao;
import com.lzx.shop.dao.UserDao;
import com.lzx.shop.dataobject.AddressDO;
import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.model.Result;
import com.lzx.shop.params.UserParam;
import com.lzx.shop.service.UserService;
import com.lzx.shop.utils.ResultUtil;
import com.lzx.shop.utils.UUIDUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName UserServiceImpl.java
 * @Description TODO
 * @createTime 2021年03月17日 23:52:00
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserDO> implements UserService {
    @Autowired
    private UserDao userDao;

    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public boolean save(UserDO userDO) {
        if (userDO == null){
            LOG.error("input userDO is null");
            return false;
        }
        String pwd = userDO.getPwd();
        // 密码加自定义盐值，确保密码安全
        String saltPwd = pwd + "_lzx2021";
        // 生成md5值，并转小写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
        userDO.setPwd(md5Pwd);
        if (!StringUtils.isBlank(userDO.getId())){
            userDO.setGmtModified(LocalDateTime.now());
            return userDao.updateById(userDO)>0;
        }
        userDO.setId(UUIDUtil.getUUID());
        userDO.setGmtCreated(LocalDateTime.now());
        userDO.setGmtModified(LocalDateTime.now());
        return userDao.insert(userDO)>0;
    }

    @Override
    public List<UserDO> findAll() {
        return userDao.selectList(null);
    }


    @Override
    public List<UserDO> getByIds(List<String> userIds) {
        return userDao.selectBatchIds(userIds);
    }

    @Override
    public UserDO findById(String id) {

        if (StringUtils.isBlank(id)){
            return null;
        }

        return userDao.selectById(id);
    }

    @Override
    public boolean update(UserDO userDO) {
        if (null ==userDO){
            LOG.error("input userdo is null");
            return false;
        }
        return userDao.updateById(userDO)>0;
    }

    @Override
    public boolean DeleteById(String id) {
        if (StringUtils.isBlank(id)){
            LOG.error("id is blank");
            return false;
        }
        return userDao.deleteById(id)>0;
    }

    @Override
    public Result Login(String mobile, String pwd) {

        if (StringUtils.isBlank(mobile)||StringUtils.isBlank(pwd)){
            LOG.error("手机号或者密码为空");

            return ResultUtil.error("201","手机号或者密码为空");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("mobile",mobile);
        UserDO userDO = userDao.selectByMap(map).get(0);

        if (userDO==null){
            LOG.error("用户不存在");
            return ResultUtil.error("202","用户不存在");
        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = pwd + "_lzx2021";
        // 生成md5值，并转小写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        if (!md5Pwd.equals(userDO.getPwd())){
            return ResultUtil.error("203","密码不正确");
        }
        return ResultUtil.success(true);
    }

    @Override
    public Result<UserDO> LoginByUserName(String userName, String pwd) {

        if (StringUtils.isBlank(userName)||StringUtils.isBlank(pwd)){
            LOG.error("用户名或者密码为空");

            return ResultUtil.error("201","用户名或者密码为空");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("user_name",userName);

        List<UserDO> userDOS = userDao.selectByMap(map);
        if (CollectionUtils.isEmpty(userDOS)) {
            LOG.error("用户不存在");
            return ResultUtil.error("202","用户不存在");
        }
        UserDO userDO = (UserDO) userDOS.get(0);

//        if (userDO==null){
//            LOG.error("用户不存在");
//            return ResultUtil.error("202","用户不存在");
//        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = pwd + "_lzx2021";
        // 生成md5值，并转小写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        if (!md5Pwd.equals(userDO.getPwd())){
            return ResultUtil.error("203","密码不正确");
        }
        return ResultUtil.success(userDO);
    }

    @Override
    public int count() {
        return userDao.selectCount(null);
    }

    @Override
    public IPage<UserDO> selectPage(Page<UserDO> page, UserParam userParam) {


        return userDao.selectPage(page,null);
    }

    @Override
    public Boolean exits(String openId) {
        Map map = new HashMap();
        map.put("openid",openId);
      List list =  userDao.selectByMap(map);
        return list.size()>0;
    }

    @Override
    public UserDO query(QueryWrapper<UserDO> queryWrapper) {
        return userDao.selectOne(queryWrapper);
    }

}
